<%@ page language="java" contentType="text/html;charset=UTF-8" pageEncoding="UTF-8"%>   
<%@ taglib uri="/struts-tags" prefix="s" %>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html>
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
	<title>角色管理-<s:if test="rid==null||rid==0">新建</s:if><s:else>编辑</s:else></title>
	<link type="text/css" rel="stylesheet" href="../<s:property value="#session.themeStyle.mainCss" default="Skin/PM/cyan/main.css"/>" />
	<link type="text/css" rel="stylesheet" href="../css/jquery.loadmask.css">
</head>
<body>
	<div class="navBar">
			当前位置：系统管理 &nbsp;&gt;&gt;&nbsp;角色权限管理 &nbsp;&gt;&gt;&nbsp;角色<s:if test="rid==null||rid==0">新建</s:if><s:else>编辑</s:else>
	</div>
	<s:form theme="simple">
	<div id="maindiv" class="tabBlock">
		<div class="editBlock">
			<table>
				<tbody>
					<tr>
						<th colspan="4">角色<s:if test="rid==null||rid==0">增加</s:if><s:else>编辑</s:else></th>
					</tr>
					<tr>
						<th style="width: 100px;">角色名称:</th>
						<td>
							<input name="rid" id="rid" type="hidden" value="${roleInfo.pkId}" />
							<s:textfield id="name" name="name" cssClass="inputText" maxlength="40"></s:textfield>
						</td>
						<th style="width: 100px;">角色机构:</th>
						<td>
							<s:select id="org" name="orgId" list="managedOrgList" listKey="orgId" listValue="name" emptyOption="true" disabled="!mapResult['isAdmin']"></s:select>
						</td>
					</tr>
					<tr>
						<th style="width: 100px;">角色描述:</th>
						<td>
							<s:textarea id="remark" name="remark" cssClass="inputText" cssStyle="height:40px;" rows="3" cols="4"></s:textarea>						
						</td>
						<th style="width: 100px;">角色授权:</th>
						<td>
							<s:if test="mapResult['roleUserList'].size<5">
						<s:iterator value="mapResult['roleUserList']" status="st">
								<span>
								<s:property value="name" /><s:if test="name!=userName">[<s:property value="userName" />]</s:if>
	<a href='javascript:void(0)' onclick='delRoleUser("<s:property value="roleInfo.pkId"/>","<s:property value="roleInfo.name" escape="false"/>",<s:property value="pkId"/>,"<s:property value="name" escape="false"/>", this); return false'>[X]</a>
								</span>
						</s:iterator>
							</s:if><s:else>
							</s:else>
							<s:if test="rid!=null&&rid>0">
							<a href='javascript:void(0)' onclick="return addRoleUser(<s:property value="rid"/>,this)"<s:if test="!mapResult['UserAuth'][pkId]"> class="disabled"</s:if>>[添加]</a>
							</s:if>
						</td>
					</tr>
					<s:if test="menuinfo!=null&&menuinfo.size()>0">
					<tr>
						<td colspan="4">
						<table>
							<s:iterator value="menuinfo" var="menuinfo">
							<tr>
								<td style="width: 120px;vertical-align:middle;">
									<input type="checkbox" id='pcf<s:property value="#menuinfo.key.pkId"/>' onclick="ckFunc(this, 'funcGroup_<s:property value="#menuinfo.key.pkId"/>')" value="<s:property value="#menuinfo.key.pkId"/>"/><s:property value="#menuinfo.key.name"/>
								</td>
								<td id="funcGroup_<s:property value="#menuinfo.key.pkId"/>">
									<ul style="list-style-type: inherit;border-width: 1px;border-color: red;">
									<s:iterator value="#menuinfo.value" status="st" var="func">
											<li style="float: left;width:320px;border: 5px green;">
								 			<s:if test="subFuncs.size>0"><!-- 有权限范围定义 -->
													<s:if test="subFuncs[0].subFuncs.size>0"><!-- 命名复合范围 -->
													<s:iterator value="#func.subFuncs" id="rangeItem">
															<s:if test="#rangeItem.subFuncs[0].subFuncs.size==0"><!-- 一维 -->
												 			<table>
												 				<tr>
												 					<td colspan="<s:property value="#rangeItem.subFuncs.size"/>" style="text-align:left;">
															 			<input type="checkbox" onclick='changeParent(this,"<s:property value="#func.parentId"/>")' value="<s:property value="#func.pkId"/>" disabled="disabled" />
															 			<s:property value="#func.name"/><!-- 功能权限 -->
												 					</td>
												 				</tr>
												 				<tr>
												 					<td colspan="<s:property value="#rangeItem.subFuncs.size"/>" style="text-align:center;"><s:property value="#rangeItem.name"/></td>
												 				</tr>
												 				<tr>
																	<s:iterator value="#rangeItem.subFuncs">
												 					<td style="text-align:center;">
														 			<input type="checkbox" onclick='changeRange(this,<s:property value="#func.pkId"/>)' value="<s:property value="pkId"/>" disabled="disabled"/>
														 			<s:property value="name"/>
												 					</td>
																		</s:iterator>
												 				</tr>
												 			</table>
															</s:if><s:else><!-- 二维 -->
															<s:if test="#rangeItem.subFuncs[1].subFuncs[0].subFuncs.size>0"><s:set name="colHead" value="#rangeItem.subFuncs[0]" /><s:set name="rowHead" value="#rangeItem.subFuncs[1]" /></s:if><s:else><s:set name="colHead" value="#rangeItem.subFuncs[1]" /><s:set name="rowHead" value="#rangeItem.subFuncs[0]" /></s:else>
												 			<table>
												 				<tr>
												 					<td colspan="<s:property value="#colHead.subFuncs.size+2"/>" style="text-align:left;">
															 			<input type="checkbox" onclick='changeParent(this,"<s:property value="#func.parentId"/>")' value="<s:property value="#func.pkId"/>" disabled="disabled"/>
															 			<s:property value="#func.name"/><!-- 功能权限 -->
												 					</td>
												 				</tr>
												 				<tr>
												 					<td colspan="2" rowspan="2" style="text-align: center;"><s:property value="#rangeItem.name"/></td>
												 					<td colspan="<s:property value="#colHead.subFuncs.size"/>" style="text-align:center;"><s:property value="#colHead.name"/></td>
												 				</tr>
												 				<tr>
																		<s:iterator value="#colHead.subFuncs">
													 					<td style="text-align:center;"><s:property value="name"/></td>
															 		</s:iterator>
												 				</tr>
																<s:iterator value="#rowHead.subFuncs" id ="rowItem"	status="rowStatus">
												 				<tr>
												 				<s:if test="#rowStatus.index==0">
												 					<td rowspan="<s:property value="#rowHead.subFuncs.size"/>" style="width: 16px;text-align: center;">
												 						<s:property value="#rowHead.name"/>
												 					</td>
												 				</s:if>
												 					<td><s:property value="#rowItem.name"/></td>
																		<s:iterator value="#rowItem.subFuncs">
													 					<td style="text-align: center;">
																 			<input type="checkbox" onclick='changeRange(this,<s:property value="#func.pkId"/>)' value="<s:property value="pkId"/>" disabled="disabled"/>
													 					</td>
															 		</s:iterator>
												 				</tr>
													 		</s:iterator>
												 			</table>
															</s:else>
														</s:iterator>
													</s:if><s:else><!-- 无名简单范围 -->
														<li>
														<ul style="list-style-type:none;">
													<s:iterator value="#func.subFuncs">
														<li style="float: left;width:80px;border: 1px green;">
											 			<input type="checkbox" onclick='changeRange(this,<s:property value="parentId"/>)' value="<s:property value="pkId"/>" disabled="disabled"/>
											 			<s:property value="name"/>
										 			</li>
										 		</s:iterator>
														</ul>
										 		</li>
													</s:else>
								 			</s:if><s:else>
									 			<table>
									 				<tr>
									 					<td style="text-align:left;">
												 			<input type="checkbox" onclick='changeParent(this,"<s:property value="#func.parentId"/>")' value="<s:property value="#func.pkId"/>" disabled="disabled"/>
												 			<s:property value="#func.name"/><!-- 功能权限 -->
									 					</td>
									 				</tr>
									 			</table>
								 			</s:else>
											</li>
							 	 </s:iterator>
									</ul>
								</td>
							</tr>
							</s:iterator>
						</table>
						</td>
						</tr>							
					</s:if>
					<tr>
					 <th id="message" colspan="4"style="color:red;text-align:left;padding-left:10px;">${message}</th>
					 </tr>
				</tbody>
			</table>
		</div>

		<div class="toolbar" style="text-align: center;">
			<a class="but28" onclick="save()" style="text-decoration:none;" >保存</a>
			<a class="but28" onclick="history.back()" style="text-decoration:none;" >返回</a>
			<a class="but28" onclick="resetFuncAndRoles()" style="text-decoration:none;" >重置</a>
			<s:hidden id="managedFuncStr" name="managedFuncStr"/>
			<input type="hidden" id="funcStr" value='<s:property value="funcStr"/>' />
			<input type="hidden" id="state" value="<s:property value="state"/>" />
		</div>
	</div>
	</s:form>
	<script type="text/javascript" src="../js/jquery.js"></script>
	<script type="text/javascript" src="../js/jquery.loadmask.min.js"></script>
	<script type="text/javascript" src="../js/modalWindow.js"></script>
	<script type="text/javascript" src="../js/store.js"></script>
	<script type="text/javascript" src="../js/RoleFuncManage.js"></script>
	<script type="text/javascript">
	
		$(document).ready(function(){
			var funcStr = $("#funcStr").val();
			if(funcStr!=""){
				var funcArr = funcStr.split(",");
				var ckBox = $(":checkbox");
				for(var i=0;i<funcArr.length;i++){
					for(var j=0;j<ckBox.length;j++){
						if(ckBox[j].value==funcArr[i]){
							$(ckBox[j]).attr("checked", true);
							break;
						}	
					}
				}
			}
			var managedFuncStr = $("#managedFuncStr").val();
			if(managedFuncStr!=""){
				var funcArr = managedFuncStr.split(",");
				var ckBox = $(":checkbox");
				for(var i=0;i<funcArr.length;i++){
					for(var j=0;j<ckBox.length;j++){
						if(ckBox[j].value==funcArr[i]){
							$(ckBox[j]).attr("disabled", false);
							break;
						}	
					}
				}
			}
		});

		function save(){
			$("#maindiv").mask("正在保存中,请稍后...");
			
			var ckBox = $(":checkbox");
			var funcStr = "";
			for(var i=0;i<ckBox.length;i++){
				if(ckBox[i].checked){
					if(funcStr==""){
						funcStr = ckBox[i].value;
					}else{
						funcStr += "," + ckBox[i].value;
					}
				}
			}
			var roleId = $("#rid").val();
			var name = $("#name").val();
			if(name==''){
				alert("角色名称不能为空！");
				return;
			}
			var remark = $("#remark").val();
			var orgId = $("#org").val();
			var state = $("#state").val();
			var url = "roleInfo-saveRoleAndFunc.action";
			$.ajax({
				url: url,
				data:{"rid":roleId,"name":name,"remark":remark,"orgId":orgId,"funcStr":funcStr},
				dataType: "json",
				success: function(result){
					if(result.success){
						alert("保存成功！");
						history.back();
					}else{
						$("#maindiv").unmask();
						alert("保存失败："+result.errorMessage);
					}
				}
			});
		}
		
		function ckFunc(obj, groupId){
			var totalCount = 0;
			var disCount = 0;
			var changeCount = 0;
			var list = $("#"+groupId).find(":checkbox");
			totalCount = list.length;
			for(var i=0;i<list.length;i++){
				var b = list[i];
				if(!b.disabled){
					if(obj.checked){
						if(!b.checked){
							b.checked=true;
							changeCount++;
						}
					}else{
						if(b.checked){
							b.checked=false;
							changeCount++;
						}
					}
				}else{
					disCount++;
				}
			}
		}
		
		function changeParent(obj,parentId){
			var cks = $(obj).parent().parent().parent().find("input:checkbox");
			var flag = false;
			for(var i=0;i<cks.length;i++){
				if($(cks[i]).attr("checked")){
					flag = true;
					break;
				}
			}
			var id = "pcf"+parentId;
			if(flag){
				$("#"+id).attr("checked",true);
			}else{
				$("#"+id).attr("checked",false);
			}
		}
		
		function changeRange(obj,parentId){
			var cks = $(obj).parent().parent().parent().find("input:checkbox");
			var flag = false;
			for(var i=0;i<cks.length;i++){
				if($(cks[i]).attr("checked")){
					flag = true;
					break;
				}
			}
			var id = "pcf"+parentId;
			if(flag){
				$("#"+id).attr("checked",true);
			}else{
				$("#"+id).attr("checked",false);
			}
		}
		
		// 删除角色授权用户
		function delRoleUser(roleId, roleName, userId, name, _this){
			if(confirm("确定要解除用户["+name+"]的["+roleName+"]授权？")){
				var roleId = $("#roleId").val(); 
				$.post("userRole-del.action", { roleId: roleId, userId: userId },
				  	function(data){
				    	$(_this).parent().remove();
				});
			}
			
		}
		
	</script>		
</body>
</html>
